var ip = returnCitySN["cip"];
var address = returnCitySN["cname"];
let loadIcon;

layui.config({
    base: path + '/layuiadmin/' //静态资源所在路径
}).extend({
    index: 'lib/index' //主入口模块
}).use(['index', 'user'], function () {
    var $ = layui.$
        , form = layui.form
    form.render();

    var formMain = $("#formMain");
    var identification = $("#identification");
    var a_wechat = $("#a_wechat");
    var a_phone = $("#a_phone");
    var a_email = $("#a_email");
    var a_user = $(".a_user");
    var item1 = $("#item1");
    var item2 = $("#item2");
    let countPhone = 0;
    let countEmail = 0;

    function countdown(obj, seconds, text) {
        var that = $(obj);
        that.attr("disabled", true);
        that.html(seconds + '秒');
        let promise = new Promise((resolve, reject) => {
            let setTimer = setInterval(
                () => {
                    seconds -= 1;
                    // console.info('倒计时:' + seconds);
                    that.html(seconds + '秒');
                    if (seconds <= 0) {
                        that.html(text);
                        resolve(setTimer)
                    }
                }
                , 1000)
        })
        promise.then((setTimer) => {
            // console.info('清除');
            clearInterval(setTimer);
            that.attr("disabled", false);
        })
    }

    a_phone.on("click", function () {
        $(formMain).html(phoneLoginHtml);
        $("#getPhoneYzm").on("click", function () {
            if (countPhone == 3) {
                layer.msg("该手机号码已锁定！请勿频繁操作！", {
                    icon: 0,
                    time: 2000
                });
                return false;
            }
            /*执行ajax发送请求向验证用户手机号码是否已绑定，如果绑定了，则进行短信发送，如果未绑定，则提示*/
            countdown($(this), 60, "重新获取");
            countPhone++;
        });
        $(identification).val("phoneLogin");
        $(item1).show();
        $(item2).hide();
    });

    a_email.on("click", function () {
        $(formMain).html(emailLoginHtml);
        $("#getEmailYzm").on("click", function () {
            if (countEmail == 3) {
                layer.msg("该邮箱账户已锁定！请勿频繁操作！", {
                    icon: 0,
                    time: 2000
                });
                return false;
            }
            /*执行ajax发送请求向验证用户邮箱是否已绑定，如果绑定了，则进行邮箱发送，如果未绑定，则提示*/
            countdown($(this), 120, "重新获取");
            countEmail++;
        });
        $(identification).val("emailLogin");
        $(item1).show();
        $(item2).hide();
    });

    a_user.on("click", function () {
        $(formMain).html(userLoginHtml);
        $(identification).val("userNameLogin");
        $(item1).hide();
        $(item2).show();
    });

    a_wechat.on("click", function () {
        layer.msg("暂未开放微信登录");
    });

    function startLoad() {
        loadIcon = layer.load(0, {
            shade: true,
            time: false,
            shade: [0.1, '#fff']
        });
    }

    function stopLoad() {
        layer.close(loadIcon);
    }

    /**
     * login
     */
    $("[lay-filter=picture-submit]").on("click", function () {

        switch ($(identification).val()) {
            case "userNameLogin":
                var username = $("#username").val();
                var password = $("#password").val();
                var remember = $(".layui-form-checked").length == 1 ? true : false;
                if (isNull(username,password)){
                    return;
                }
                startLoad();
                $.ajax({
                    url: path + "/doLogin.json"
                    , type: "POST"
                    , dataType: "JSON"
                    , data: {
                        "userName": username, "passWord": password, "remember": remember,
                        "ip": ip, "address": address
                    }
                    , success: function (res) {
                        stopLoad();
                        if (res.code == "0") {
                            layer.msg(res.msg, {
                                icon: 2,
                                time: 2000
                            });
                        } else {
                            layer.msg(res.msg, {
                                offset: '9px'
                                , icon: 1
                                , time: 1000
                            }, function () {
                                location.href = path + "/backstage/home.html";
                            });
                        }
                    }, error: function () {
                        stopLoad();
                        layer.msg("认证接口出现异常,请联系管理员！", {
                            icon: 2,
                            time: 2000
                        });
                    }
                });
                break;
            case "phoneLogin":
                var phone = $("#phone").val();
                var phoneYzm = $("#phoneYzm").val();
                if (isNull(phone,phoneYzm)){
                    return;
                }
                layer.msg("执行手机号码验证登录");
                break;
            case "emailLogin":
                var email = $("#email").val();
                var emailYzm = $("#emailYzm").val();
                console.log("邮箱：" + email + "/验证码：" + emailYzm)
                layer.msg("执行邮箱验证登录");
                break;
        }
    });


});

